package com.itheima.controller;


import com.itheima.common.PageBean;
import com.itheima.common.Result;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Component
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping
    public Result<?> insertOne(@RequestBody Student student){
        studentService.insertOne(student);
        return Result.success();
    }

    /**
     * 删除一个学生
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteOne(@PathVariable Integer id){
      studentService.deleteOne(id);

      return Result.success();
    }

    /**
     * 更新一个学生信息
     * @param student
     * @return
     */
    @PutMapping
    public Result<?> updateOne(@RequestBody Student student){
        studentService.updateOne(student);
        return Result.success();
    }

    /**
     * 修改学生违纪分数
     * @param student
     * @return
     */
    @PutMapping("/deductions")
    public Result<?> updateDscore(@RequestBody Student student  ){

    studentService.updateDscore(student);

    return Result.success();
    }

    /**
     * 查询一个学生
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<?> selectOne(@PathVariable Integer id){
        Student student = studentService.selectOne(id);

        return Result.success(student);
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @param highestDegree
     * @param classesId
     * @param studentNumber
     * @return
     */
    @GetMapping
    public Result<PageBean<Student>> selectPage(
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "10")Integer pageSize,
            String name,
            String highestDegree,
            String classesId,
            String studentNumber
    ){
        PageBean<Student> studentPageBean = studentService.selectPage(
                page,pageSize,name,highestDegree,classesId,studentNumber
        );

        return Result.success(studentPageBean);

    }



}
